package com.office.web;

import com.office.bean.Depart;
import com.office.service.DepartService;
import com.office.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DepartController {

    @Autowired
    DepartService departService;

    @RequestMapping("/dep_add")
    public void addDepart(Depart depart, HttpServletResponse response) throws Exception{

        response.setContentType("text/html;charset=utf-8");
        int i = departService.addDepart(depart);
        if (i>0){
            response.getWriter().write("<script>alert('添加成功');location.href='/depart_list?pageIndex=1&pageSize=5';</script>");
        }else {
            response.getWriter().write("<script>alert('添加失败');location.href='/html_departadd';</script>");
        }
    }

//    @RequestMapping("/find_dep")
//    @ResponseBody
//    public List<Depart> find(){
//        return departService.findDepartsByPage();
//    }
    @RequestMapping("/depart_list")
    public String list(long pageIndex, long pageSize, Model model){
        long totalcount = departService.findTotalcount();
        Map<String , Object> map = new HashMap<>();
        map.put("pageStart",(pageIndex-1)*pageSize);
        map.put("pageSize",pageSize);
        List<Depart> departsByPage = departService.findDepartsByPage(map);
        for (Depart depart : departsByPage) {
            long empCount = departService.findEmpCount((int) depart.getId());
            depart.setEmpCount(empCount);
        }
        PageUtils<Depart> pageUtils = new PageUtils<Depart>(pageIndex,pageSize,totalcount,departsByPage);
        model.addAttribute("pageUtils",pageUtils);
        return "departlist";
    }


    @RequestMapping("/delete_dep")
    @ResponseBody
    public boolean delete_dep(int id){
        return departService.deleteById(id);

    }

    @RequestMapping("/depart_update/{id}")
    public String depart_update(@PathVariable("id")  int id ,Model model){
        Depart depart = departService.findDepById(id);
        model.addAttribute("depart",depart);

        return "departupdate";
    }

    @RequestMapping("/departupdate")
    public void departupdate(Depart depart,HttpServletResponse response) throws Exception{
        response.setContentType("text/html;charset=utf-8");
        int update = departService.update(depart);
        if (update>0){
            response.getWriter().write("<script>alert('更新成功');location.href='/depart_list?pageIndex=1&pageSize=5';</script>");

        }else {
            response.getWriter().write("<script>alert('更新失败');location.href='/depart_update/"+depart.getId()+"';</script>");

        }
    }
}
